<?php

/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/10/15
 * Time: 9:24
 */

namespace App\Services\Api;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\OrderBaleModel;
use App\Models\Admin\OrderCheckModel;
use App\Models\Admin\OrderTempModel;
use App\Models\LogModel;
use App\Models\OrderCommentModel;
use App\Services\OrdersPayService;
use Illuminate\Http\Request;
use App\Models\MoneyBilModel;
use DB;
use App\Models\User\UserOrdersModel;
use App\Models\User\UserLevelModel;

/// App\Services\Api\UserLevel@
class UserLevel extends ApiBase
{

    public function setuserLevel(Request $request)
    {
        $uid=OA::$user->getId();
        $moneyval = (int)$request->input('moneyval');///变更的价钱 如果是转入是负数,转出是正数
        $leveList = UserLevelModel::getLevelInfo();
        $myLeveInfo = UserLevelModel::getUserLevel($uid);
        $money = $myLeveInfo['info']['money'];    ///当前用户等级的价额
        $nowselectType = $request->input('level');
        $nowselect = $leveList[$nowselectType];
        $selectMoney = $nowselect['money'];////当前选择的价额
        $jisuan = $money - $selectMoney;//后台计算  变更的价钱 如果是转入是负数,转出是正数
        if ($moneyval !== $jisuan) {
            OA::Json(1, "moneyval参数错误");
        }
        if ($jisuan == 0) {
            OA::Json(1, "已经修改");
        }
        $userMoney = OA::$user->getMoney();
        if ($jisuan < 0) {///小于0表示是转入升级
            $xuyaoMoney = 0 - $jisuan;///转成正数
            if ($userMoney < $xuyaoMoney) {
                OA::Json(10, "余额不足,请充值");
            }
            try {
                DB::beginTransaction();
                $val = MoneyBilModel::zhuanzhangAtoB($uid, MoneyBilModel::system_uid, $xuyaoMoney, "从(" .
                    $myLeveInfo['info']['name'] . ")升级到(" . $nowselect['name'] . "),转入了" . $xuyaoMoney . "元");
                if($val&&UserLevelModel::upType($uid,$nowselectType)){
                    DB::commit();
                    OA::Json(0, "修改成功");
                }else{
                    throw new \Exception('保存等级错误');
                }

            } catch (\Exception $e) {
               /// $this->msgerror = $e->getMessage();
                DB::rollback();
                OA::Json(1, "系统错误".$e->getMessage().",请重试");
            }
        }else{///////如果是正数,则说明是降低
            $order=$this->getUserOrderCount($uid);///用户用户未完成订单数量
            if($order!==0){
                OA::Json(1, "有尚未完成的订单,暂时不允许降级;请尽快完成全部订单");
            }

            try {
                DB::beginTransaction();
                $val = MoneyBilModel::zhuanzhangAtoB(MoneyBilModel::system_uid,$uid, $jisuan, "从(" .
                    $myLeveInfo['info']['name'] . ")降级到(" . $nowselect['name'] . "),转出了" . $jisuan . "元");
                if($val&&UserLevelModel::upType($uid,$nowselectType)){
                    DB::commit();
                    OA::Json(0, "修改成功");
                }else{
                    throw new \Exception('保存等级错误');
                }

            } catch (\Exception $e) {
                /// $this->msgerror = $e->getMessage();
                DB::rollback();
                OA::Json(1, "系统错误".$e->getMessage().",请重试");
            }

        }
        OA::Json(1,'系统错误,请稍候');


    }



    private function getUserOrderCount($uid){
        return UserOrdersModel::where([
            'fid'=>$uid
        ])->where('status','!=',UserOrdersModel::status_wancheng)->count();
    }


}
